<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\MovieBaseInfo;
use App\Jobs\CrawlerMovieJobs;

class GetMovieInfo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'daka:get-movie-info 
                            {--id=0 : 采集指定 ID 的电影详情（id 来自豆瓣）}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '抓取指定 ID 电影详情';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $id = $this->option('id');
        $run = false;
        if ($id <= 0) {
            if ($m = MovieBaseInfo::where('is_crawler', false)->where('crawler_count', '<', 3)->first())
            {
                $id = $m->douban_id;
                $run = true;
            } else {
                $this->info('没有可采集的项目');
            }
        } else {
            $run = true;
        }
        if ($run) dispatch(new CrawlerMovieJobs($id));
    }
}